<?php
namespace App\Libs\Common;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class ZtwController extends Controller
{	
    
     //郑投网  (传用户和订单信息)
    public  function ztw_user_order($order_id)
    {   return true;
        header("Content-type: text/html; charset=utf-8");
        
        $row=M("orders as a")->join("left join goods as b on a.goods_id=b.goods_id")->where("a.order_id='$order_id'")->field("b.goods_purchase_price,a.user_name,a.user_phone,a.user_id_number,a.goods_name,a.goods_order_attr,a.goods_price,a.order_gross_rent,a.order_monthly_rent,a.order_tenancy_term,a.zm_credit_score,a.order_create_time,a.order_comefrom,a.order_status")->find();

        $param['order_id']=$order_id;//订单号
        $param['user_name']=$row["user_name"];//用户名
        $param['user_phone']=$row["user_phone"];//手机号
        $param['user_id_card']=$row["user_id_number"];//身份证
        $param['goods_name']=$row["goods_name"];//商品名称
        $param['goods_order_attr']=$row["goods_order_attr"];//商品属性
        $param['goods_price']=$row["goods_price"];//商品总价
        $param['order_gross_rent']=$row["order_gross_rent"];//总租金
        $param['order_monthly_rent']=$row["order_monthly_rent"];//月租(每月价格）
        $param['order_tenancy_term']=$row["order_tenancy_term"];//租期(月)
        $param['credit_score']=$row["zm_credit_score"];//信用分
        $param['create_time']=$row["order_create_time"];//下单时间
        $param['goods_pur_price']=$row["goods_purchase_price"];//商品进价

        if($row["order_comefrom"] != "H5_JD")
        {
            $order_comefrom='XCX_ZFB';
        }else{
            $order_comefrom='H5_JD';
        }
        $param['order_comefrom']=$order_comefrom;//订单来源
        $param['order_status']=$row["order_status"];//订单状态
       
        $url='http://117.158.208.96:10102/thirdPerson/personalInfo';

        $param=json_encode($param,JSON_UNESCAPED_UNICODE);
        
        $result=$this->request_post($url,$param);
        
        $ztw_res=json_decode($result,true);
       
        if ($ztw_res["status"] == 'SUCCESS') 
        { 
            $res=M("orders")->where("order_id= '$order_id'")->save(array("is_delivery"=>2));
        }
        
    }



     /* 郑投网 (分期数据) 
    *  stages_id 分期表id
    *  payment   实付金额
    *  device    订单来源  
    */
    public function ztw_order_stages($stages_id,$payment,$device)
    {   return true;
        //$id 分期表id
        header("Content-type: text/html; charset=utf-8");
      
        $row=M("order_stages as a")->join("orders as b on a.order_id=b.order_id")->where("a.id='$stages_id'")->field("a.ztw_status,a.order_id,a.current_tenancy_term,a.monthly_rent,a.repayment_time,a.last_repayment_time,b.is_delivery")->find();

        $param['stages_id']=$stages_id;//分期表id
        $param['order_id']=$row["order_id"];//订单号
        $param['now_term']=$row["current_tenancy_term"];//当前期
        $param['payable']=$row["monthly_rent"];//应付金额
        $param['payment']=$payment;//实付金额
        $param['repayment_time']=$row["repayment_time"];//账单日
        $param['pay_time']=$row["last_repayment_time"];//支付时间
        
        //第三方单号
        if($device == "H5_JD" || $device == "H5_ZFB")
        {
             $param['out_order_id']=$stages_id;
        }else{
            $log=M("log_xcx")->where("out_trans_no='$stages_id'")->getField("log");
            $arr=json_decode($log,true);
            $param['out_order_id']=$arr["alipay_fund_order_no"];
        }

        //已传递过的数据不再重复传递或非郑投网订单
        if ($row['ztw_status']==2 || $row['is_delivery']==1) return false;

        $url='http://117.158.208.96:10102/thirdTrade/payItem';

        $param=json_encode($param,JSON_UNESCAPED_UNICODE);

        $result=$this->request_post($url,$param);
        
        //修改 ztw_status 状态
        $ztw_res=json_decode($result,true);

        if ($ztw_res["status"] == 'SUCCESS')
        {   
            $data["ztw_status"]=2;
            M("order_stages")->where("id='$stages_id'")->save($data);
        }
    }


    //审核
    public function ztw_audit($order_id)
    {  
        header("Content-type: text/html; charset=utf-8");
        //订单号
        $url='http://117.158.208.96:10102/thirdPerson/queryPersonalRisk/'.$order_id;

        //$param=json_encode($param,JSON_UNESCAPED_UNICODE);
        
        $result=$this->request_post($url);
        
        $ztw_res=json_decode($result,true);

        //print_r($ztw_res);
        return $ztw_res;
        //PASS ：通过 REVIEW ：需要人工再审核 REJECT:拒绝
        // if ($ztw_res["data"]["final_decision"] == "PASS") 
        // {
        //     echo 1;
        // }elseif($ztw_res["data"]["final_decision"] == "REVIEW"){
        //     echo "需要人工再审核";
        // }else{
        //     $ztw_res["data"]["desc"];
        // }
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    /**
     * 模拟POST请求
     * @param  string $url   [请求地址]
     * @param  string $param [参数]
     */
    private function request_post($url = '', $param = '') 
    {      
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
                'Content-Type: application/json; charset=utf-8',
        ));

        $data = curl_exec($ch);//运行curl

        if($data === false)
        {
            return  'Curl error: ' . curl_error($ch);
        }
        
        curl_close($ch);
        
        return $data;
    }



}